package com.example.springboot.service.impl;

import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.Page;
import com.example.springboot.service.CommentService;
import com.example.springboot.utils.CommentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

/**
 * 功能：

 * 日期：2024/3/18 20:12
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired(required = false)
    private CommentMapper commentMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private BlockedWordMapper blockedWordMapper;

    /**
     * 获取评论列表
     *
     * @param productId
     * @return
     */
    @Override
    public List<Comment> getCommentList(Long productId) {
        List<Comment> list = commentMapper.getCommentList(productId);
        return CommentUtils.processComments(list);
    }

    /**
     * 获取评论列表总数
     *
     * @param productId
     * @return
     */
    @Override
    public Long getCommentListTotal(Long productId) {
        return commentMapper.getCommentListTotal(productId);
    }

    /**
     * 获取最新评论
     *
     * @return
     */
 /*   @Override
    public List<Comment> getLatestComments() {
        return commentMapper.getLatestComments();
    }*/

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @Override
    public boolean save(Comment comment) {
        User user = userMapper.selectById(comment.getUserId().intValue());
//        comment.setUserName(user.getUsername());

        // 这里实现对评论内容的检查，如果有屏蔽词，那么需要替换为*
        List<BlockedWord> blockedWords = blockedWordMapper.getAllValid();
        // 使用流操作获取word集合
        List<String> words = blockedWords.stream().map(BlockedWord::getWord).distinct().collect(Collectors.toList());
        for (String word : words) {
            // 替换评论的内容中的屏蔽词改为被修改文字长度的*
            comment.setContent(comment.getContent().replaceAll(word, "**"));
        }
        comment.setCreateTime(new Date());
        comment.setIsDelete(0);
        return commentMapper.save(comment) > 0;
    }

    /**
     * 删除评论
     *
     * @param comment
     * @return
     */
    @Override
    public boolean removeComment(Comment comment) {
        Queue<Comment> queue = new LinkedList<>();
        queue.offer(comment);
        while (!queue.isEmpty()) {
            Comment cur = queue.poll();
            int resultNum = commentMapper.removeById(cur.getId());
            if (resultNum <= 0) return false;
            if (cur.getChild() != null) {
                List<Comment> child = cur.getChild();
                for (Comment tmp : child)
                    queue.offer(tmp);
            }
        }
        return true;
    }

    @Override
    public Page<Comment> selectInPage(Comment comm, Integer currentPage, Integer pageSize) {
        try {
            Integer startRow = (currentPage - 1) * pageSize;
            List<Comment> commentList = commentMapper.select(comm);
            int total = commentList.size();
            List<Comment> comments = commentMapper.selectInPage(comm, startRow, pageSize);
            Page<Comment> page = new Page<>();
            page.setList(comments);
            page.setTotal(total);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询评论失败");
        }
    }

    @Override
    public boolean removeComments(List<Comment> comments) {
        try {
            for (Comment comment : comments) {
                Queue<Comment> queue = new LinkedList<>();
                queue.offer(comment);
                while (!queue.isEmpty()) {
                    Comment cur = queue.poll();
                    int resultNum = commentMapper.removeById(cur.getId());
                    if (resultNum <= 0) return false;
                    if (cur.getChild() != null) {
                        List<Comment> child = cur.getChild();
                        for (Comment tmp : child)
                            queue.offer(tmp);
                    }
                }
            }
            return true;
        } catch (Exception e) {
            throw new ServiceException("删除评论失败");
        }
    }
}
